package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.guoning.office_system.model.Department;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.DepartmentService;
import com.guoning.office_system.service.UserService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController {
    @Autowired
    DepartmentService departmentService;
    @Autowired
    UserService userService;

    /**
     * 条件查询（分页）
     *
     * @param pageIndex
     * @param pageSize
     * @param id
     * @param userId
     * @param codeOrName
     * @return
     */
    @RequestMapping(value = "/select")
    public JsonResult select(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "id", defaultValue = "") Long id,
                             @RequestParam(value = "userId", defaultValue = "") Long userId,
                             @RequestParam(value = "codeOrName", defaultValue = "") String codeOrName) {
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("id", id);
            criteria.addExtField("userId", userId);
            criteria.addExtField("codeOrName", codeOrName);
            pageBean = departmentService.selectByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 新增部门
     *
     * @param request
     * @param department
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, Department department) {
        JsonResult jsonResult;
        try {
            if (department.getUserId() == null)
                return new JsonResult(Constants.STATUS_FAIL, "请选择部门负责人");
            if (department.getDepartmentCode() == null)
                return new JsonResult(Constants.STATUS_FAIL, "请输入部门编码");
            if (department.getDepartmentName() == null)
                return new JsonResult(Constants.STATUS_FAIL, "请输入部门名称");
            if (department.getRemark() == null)
                return new JsonResult(Constants.STATUS_FAIL, "请输入部门备注");
            Criteria criteria = new Criteria();
            criteria.addExtField("codeOrName", department.getDepartmentCode());
            List<Department> list0 = departmentService.selectAll(criteria);
            if (list0.size() != 0)
                return new JsonResult(Constants.STATUS_FAIL, "部门编码或名称已存在，请检查");
            criteria.addExtField("codeOrName", department.getDepartmentName());
            List<Department> list1 = departmentService.selectAll(criteria);
            if (list1.size() != 0)
                return new JsonResult(Constants.STATUS_FAIL, "部门编码或名称已存在，请检查");
            User user = getUserDate(request);
            department.setCreator(user.getUserName());
            Long departmentId = departmentService.insertSelective(department);
            User user1 = new User();
            user1.setId(department.getUserId());
            user1.setDepartmentId(departmentId);
            user1.setDepartmentCode(department.getDepartmentCode());
            user1.setDepartmentName(department.getDepartmentName());
            userService.updateByPrimaryKeySelective(user1);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "新增成功");
        } catch (Exception e) {
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "新增异常");
        }
        return jsonResult;
    }

    /**
     * 删除
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult delete(HttpServletRequest request, String params) {
        JsonResult jsonResult;
        try {
            List<Department> list = JSONArray.parseArray(params, Department.class);
            User user = getUserDate(request);
            Criteria criteria = new Criteria();
            criteria.addExtField("modifier", user.getUserName());
            criteria.addExtField("list", list);
            int i = departmentService.updateFordelete(criteria);
            userService.deleteDepartment(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
        } catch (Exception e) {
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 修改
     * @param request
     * @param department
     * @return
     */
    @RequestMapping(value = "/update")
    public JsonResult update(HttpServletRequest request, Department department){
        JsonResult jsonResult;
        try{
            User user = getUserDate(request);
            department.setModifier(user.getUserName());
            department.setModifiedTime(new Date());
            int i = departmentService.updateByPrimaryKeySelective(department);
            User user1 = new User();
            user1.setId(department.getUserId());
            user1.setDepartmentId(department.getId());
            user1.setDepartmentCode(department.getDepartmentCode());
            user1.setDepartmentName(department.getDepartmentName());
            userService.updateByPrimaryKeySelective(user1);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "修改成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "修改异常");
        }
        return jsonResult;
    }
}
